home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 30 / Mac Magazin and MacEasy Magazine CD - Issue 30.iso / utilities / Mac OS X / Flurry / Flurry source / FlurryView.m < prev    next >
Text File  |  2002-01-25  |  4KB  |  181 lines

  1. //
  2. //  hotView.m
  3. //  hot
  4. //
  5. //  Created by calumr on Sat Jul 07 2001.
  6. //  Copyright (c) 2001 __CompanyName__. All rights reserved.
  7. //
  8.  
  9. #import <OpenGL/glu.h>
  10. #import "FlurryView.h"
  11. #import "Gl_saver.h"
  12.  
  13. typedef enum _ColorModes
  14. {
  15.     redColorMode,
  16.     magentaColorMode,
  17.     blueColorMode,
  18.     cyanColorMode,
  19.     greenColorMode,
  20.     yellowColorMode,
  21.     slowCyclicColorMode,
  22.     cyclicColorMode,
  23.     tiedyeColorMode,
  24.     rainbowColorMode,
  25.     whiteColorMode,
  26.     multiColorMode,
  27.     darkColorMode
  28. } ColorModes;
  29.  
  30. extern ColorModes currentColorMode;
  31. extern float incohesion;
  32. extern float colorIncoherence;
  33. extern float streamSpeed;
  34. extern float fieldCoherence;
  35. extern float fieldSpeed;
  36. extern int numParticles;
  37. extern int starSpeed;
  38. extern float seraphDistance;
  39. extern float streamSize;
  40. extern float streamExpansion;
  41. extern float fieldRange;
  42. extern float streamBias;
  43. extern int numStreams;
  44.  
  45. @implementation FlurryView
  46.  
  47. - (id)initWithFrame:(NSRect)frameRect isPreview:(BOOL) preview
  48. {
  49.     NSUserDefaults *defaults = [ScreenSaverDefaults defaultsForModuleWithName:@"Flurry"];
  50.     
  51.     numStreams = MAX(5,[defaults integerForKey:@"numStreams"]);
  52.     streamExpansion = MAX(1,[defaults integerForKey:@"streamExpansion"]);
  53.     currentColorMode = [defaults integerForKey:@"currentColorMode"];
  54.     
  55.     _initedGL = NO;
  56.     
  57.     if (self = [super initWithFrame:frameRect isPreview:preview]) {
  58.         NSOpenGLPixelFormatAttribute attribs[] =
  59.         {
  60.             NSOpenGLPFAAccelerated,
  61.             NSOpenGLPFADepthSize, 0,
  62.             NSOpenGLPFAColorSize, 32,
  63.             //NSOpenGLPFAMinimumPolicy,
  64.             NSOpenGLPFAClosestPolicy,
  65.                 0
  66.         };
  67.         NSOpenGLPixelFormat *format = 
  68.             [[[NSOpenGLPixelFormat alloc] initWithAttributes:attribs] autorelease]; 
  69.         _view = [[[NSOpenGLView alloc] initWithFrame:NSZeroRect pixelFormat:format] autorelease];
  70.         [self addSubview:_view];
  71.     }
  72.  
  73.     return self;
  74. }
  75.  
  76. - (void)dealloc
  77. {
  78.     [_view removeFromSuperview];
  79.     [super dealloc];
  80. }
  81.  
  82. - (void)setFrameSize:(NSSize)newSize
  83. {
  84.     [super setFrameSize:newSize];
  85.     [_view setFrameSize:newSize];
  86.     [[_view openGLContext] makeCurrentContext];
  87.  
  88.     glClearColor(0.0,0.0,0.0,1.0);
  89.     glClear(GL_COLOR_BUFFER_BIT);
  90.     glFlush();
  91.     _initedGL = NO;
  92. }
  93.  
  94. - (void)startAnimation
  95. {
  96.     NSOpenGLContext *context;
  97.     [super startAnimation];
  98.     context = [_view openGLContext];
  99.     [context makeCurrentContext];
  100.     glClearColor(0.0, 0.0, 0.0, 0.0);
  101.     glClear(GL_COLOR_BUFFER_BIT);
  102.     glFlush();
  103. }
  104.  
  105. - (void)animateOneFrame
  106. {
  107.     int Width, Height;
  108.     
  109.     Width = (int)[self frame].size.width;
  110.     Height = (int)[self frame].size.height;
  111.     
  112.     [[_view openGLContext] makeCurrentContext];
  113.     
  114.     [_view lockFocus];
  115.     
  116.     if (!_initedGL)
  117.     {
  118.         GLSetupRC();
  119.         
  120.         MakeTexture();
  121.         
  122.         _initedGL = YES;
  123.     }
  124.     
  125.     GLResize(Width, Height);
  126.     
  127.     GLRenderScene();
  128.     
  129.     glFlush();
  130.     
  131.     [_view unlockFocus];
  132. }
  133.  
  134. - (void)stopAnimation
  135. {
  136.     // Do your animation termination here
  137.     _initedGL = NO;
  138.     [super stopAnimation];
  139. }
  140.  
  141. - (BOOL) hasConfigureSheet
  142. {
  143.     return YES;
  144. }
  145.  
  146. - (NSWindow*)configureSheet { 
  147.  
  148.     [NSBundle loadNibNamed:@"Flurry.nib" owner:self];
  149.     
  150.     [streamCountSlider setIntValue:numStreams];
  151.     [colourMenu selectItemAtIndex:currentColorMode];
  152.     [thicknessSlider setFloatValue:sqrt(streamExpansion)];
  153.     
  154.     return window;
  155. }
  156.  
  157. - (IBAction)changeColourMode:(id)sender
  158. {
  159.     currentColorMode = [colourMenu indexOfSelectedItem];
  160. }
  161.  
  162. - (void)closeSheet:(id)sender
  163. {
  164.     NSUserDefaults *defaults = [ScreenSaverDefaults defaultsForModuleWithName:@"Flurry"];
  165.     
  166.     numStreams = [streamCountSlider intValue];
  167.     streamExpansion = [thicknessSlider intValue];
  168.     streamExpansion *= streamExpansion;
  169.     currentColorMode = [colourMenu indexOfSelectedItem];
  170.     
  171.     [defaults setInteger:numStreams forKey:@"numStreams"];
  172.     [defaults setInteger:streamExpansion forKey:@"streamExpansion"];
  173.     [defaults setInteger:currentColorMode forKey:@"currentColorMode"];
  174.  
  175.     [defaults synchronize];
  176.     
  177.     [NSApp endSheet:window];
  178. }
  179.  
  180. @end
  181.